//
//  NewFeatureViewController.swift
//  weibo
//
//  Created by 王、小胜 on 16/7/27.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
//可重用cellId
private let FeatureViewCellId = "FeatureViewCellId"

private let FeatureImageCount = 4

class NewFeatureViewController: UICollectionViewController {
    
    //MARK： -构造函数
    init() {
        //super.制定构造函数
        
        let layout = UICollectionViewFlowLayout()
        //格子属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //关闭状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //注册可重用cell
        self.collectionView!.registerClass( NewFeatureCell.self, forCellWithReuseIdentifier: FeatureViewCellId)

        
    }
   
    // MARK: UICollectionViewDataSource
    //每个分组中格子的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return FeatureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(FeatureViewCellId, forIndexPath: indexPath)as! NewFeatureCell
    
        //cell背景颜色
       cell.imageIndex = indexPath.item
    
        return cell
    }
    
    ///Scrollview 停止滚动的方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //最后一页才调用动画
        //计算页数
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        //判断是否是最后一页
        if page != FeatureImageCount - 1 {
            return
        }
        
        //cell播放动画
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0))as! NewFeatureCell
        
        //显示动画
        cell.showButtonAnim()
    }

}

//MARK: - 新特性cell
private class NewFeatureCell: UICollectionViewCell {
    ///图像属性
    private var imageIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named:"new_feature_\(imageIndex + 1)")
            
            //隐藏按钮
            startButton.hidden = true
        }
    }
    
    ///开始体验按钮的点击方法
    @objc private func clickStartButton() {
        print("快点开始爽吧")
    }
    
    //frame】大小由layout。itemSize制定
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //1.添加控件
        addSubview(iconView)
        addSubview(startButton)
        //2.制动位置
        iconView.frame = bounds
        
        //按钮位置
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        //3.添加监听方法
        
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    ///显示按钮动画
    private func showButtonAnim() {
        
        //显示按钮
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        //动画结束前禁止用户交互
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(5.0,//动画时长
            delay: 0,                  //延时时间
            usingSpringWithDamping: 0.8, //弹力系数，0-1，越小越弹
            initialSpringVelocity: 10,//初始速度，模拟重力加速度
            options: [],              //动画选项
            animations: { () -> Void in
            
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
                //动画结束后打开用户交互
                self.startButton.userInteractionEnabled = true
        }
        
    }
    
    //懒加载控件
    //图像
    private lazy var iconView: UIImageView = UIImageView()
    //按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), imageName: "new_feature_finish_button")
}
